<?php 
if(isset($_GET['type']) && is_numeric($_GET['type'])){
	include("dbconfig.php");
	include('header.php');
	//เก็บตัวแปรหน้า
	if(isset($_GET['page']))
	{
		$page= $_GET['page'];
		$index = 1;
	}else{
		$page = 1;		
	}

	$type = $_GET['type'];
	if($type==1)
		$title = "วิเคราะห์และรายงาน";
	else if($type==4)
		$title = "ปาตานีคาเฟ่";
	else if($type==5)
		$title = "บรรณาธิการ";
	else if($type==6)
		$title = "Quote";
	else if($type==7)
		$title = "ภาคสนามปาตานี";
    else if($type==8)
		$title = "การ์ตูน";
	else if($type==9)
		$title = "สื่อสิ่งพิมพ์";
	else if($type==10)
		$title = "Tupat Tube";
	else if($type==11)
		$title = "status of the day";
	else if($type==12)
		$title = "ประชาสัมพันธ์";

?>
	<div class="prl-container">
		<div class="prl-grid">
			<section class="prl-span-8">
				<div class="prl-article w-box prl-panel">
					<div class="w-box-inner">
						<h4 class="prl-category-title"><?php echo $title?></h4>
					</div>
				</div>
				<?php 
				$sql = "SELECT * FROM article WHERE fix_status = 1 AND fix_article_type =".$type." ORDER BY inserted_date_time DESC";
				
				$rsd = mysql_query($sql);
				$count = mysql_num_rows($rsd);
				$per_page = 10;
				$pages = ceil($count/$per_page);
				$contents = array();
				$index = $page*10-10;
	
				while($row = mysql_fetch_array($rsd))
				{
					array_push($contents, $row);
				} 
				for($i=0;$i<10;$i++) {					
					$last_key = key( array_slice( $contents, -1, 1, TRUE ) );
					if($index <= $last_key){
						$value = $contents[$index];
						$index++;
						$date = new DateTime($value['inserted_date_time']);
				?>
						<article class="prl-article w-box prl-panel">
							<div class="w-box-inner">

							<?php 
								if((bool)preg_match('#<img[^>]+src=[\'"]([^\'"]+)[\'"]#', html_entity_decode($value['description']), $matches))
								{
								   $imageFound = $matches[0];
								   $src = substr($imageFound, strpos($imageFound, "src"));
							?>
									<a class="thumbnail popup-link-image" href=<?php echo substr($src,4,strlen($src))?>>						
										<span class="prl-overlay">
											 <img <?php echo $src;?> alt="Patani Forum"/ >
											<span class="prl-overlay-area o-photo"></span>
										</span>
									</a>
							<?php
								}else if((bool)preg_match('#<iframe[^>]+src=[\'"]([^\'"]+)[\'"]#', html_entity_decode($value['description']), $matches))
								{
									$youtubeFound = $matches[0];
									$src = substr($youtubeFound, strpos($youtubeFound, "src"));
							?>
									<iframe <?php echo $src;?> width="500" height="281"></iframe>
							<?php
								}
							?>
								

								<h3><a href="single.php?id=<?php echo $value["article_id"]?>" target="_blank"><?php echo $value["title"]?></a> <span class="prl-badge red"><?php echo $title;?></span></h3> 
								<div class="prl-article-meta"><span><i class="fa fa-clock-o"></i> <?php echo $date->format('d/m/Y H:i:s');?></span></div>
								<p><?php echo mb_substr(strip_tags(html_entity_decode($value['description'])),0,200,'UTF-8');?></p>
								</div>	
								<div class="prl-article-bottom clearfix">
									<a href="single.php?id=<?php echo $value["article_id"]?>" target="_blank" class="prl-button light floatright">เพิ่มเติม</a>
								</div>
						</article>
				
				<?php
					}else{
						break;
					}
				}
				?>
				
				
				<div class="prl-article w-box prl-panel">
					<div class="w-box-inner">
						<div class="prl-pagin prl-pagin-center">
							<?php
								for($i=1;$i<=$pages;$i++){
									if($i == $page)
										echo "<a href=\"category.php?type=$type&page=$i\" class=\"active\">$i</a>";
									else 
										echo "<a href=\"category.php?type=$type&page=$i\">$i</a>";
								}
							?>
						</div>
					</div>
				</div>
				
			</section>
			<aside id="sidebar" class="prl-span-4">
				<?php include ('sidebar.php');?>
			</aside>
		</div><!--.prl-grid -->
	</div><!-- .container-->

<?php 
	include ('footer.php');
}else{
	header("Location: http://www.pataniforum.com");
}?>